<?php

namespace app\rent\controller\Product;

use think\Controller;

class Order extends Controller
{
    // 租赁模型
    protected $LeaseModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->LeaseModel = model('Product.Lease');
    }

    public function index()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $page = $this->request->param('page', 1, 'trim');

        $status = $this->request->param('status', 0, 'trim');

        $business = model('Business.Business')->find($busid);

        if (!$business) {
            $this->error('用户不存在');
        }

        $limit = 2;

        $start = ($page - 1) * $limit;

        // 定义条件数组
        $where = [
            'busid' => $busid
        ];

        // 判断订单状态是否为真
        if ($status) {
            $where['lease.status'] = $status;
        }

        $LeaseData = $this->LeaseModel->with(['product'])->where($where)->limit($start, $limit)->order('createtime DESC')->select();

        if ($LeaseData) {
            $this->success('查询成功', null, $LeaseData);
        } else {
            $this->error('暂无数据', null, []);
        }
    }

    public function confirm()
    {
        $busid = $this->request->param('busid', 0, 'trim');
        $leaseid = $this->request->param('leaseid', 0, 'trim');

        $business = model('Business.Business')->find($busid);

        if (!$business) {
            $this->error('用户不存在');
        }

        $lease = $this->LeaseModel->find($leaseid);

        if (!$lease) {
            $this->error('订单不存在');
        }

        if ($lease['status'] == 3) {
            $this->error('无须重复确认收货');
        }

        $data = [
            'id' => $leaseid,
            'status' => 3
        ];

        $result = $this->LeaseModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('确认收货失败');
        } else {
            $this->success('确认收货成功');
        }
    }

    public function recovery()
    {
        $id = $this->request->param('id', 0, 'trim');

        $lease = $this->LeaseModel->find($id);

        if (!$lease) {
            $this->error('租赁订单不存在');
        }

        $factory = [
            'contact' => config('site.contact'),
            'tel' => config('site.tel'),
            'address' => config('site.address')
        ];

        $Product = model('Product.Product')->find($lease['proid']);

        $expList = model('Expressquery.Expressquery')->column('id,name');

        $expData = [];

        foreach ($expList as $key => $item) {
            $expData[] = [
                'text' => $item,
                'value' => $key
            ];
        }

        $data = [
            'factory' => $factory,
            'product' => $Product,
            'expData' => $expData,
            'lease' => $lease
        ];

        $this->success('查询成功', null, $data);
    }

    public function restore()
    {
        $id = $this->request->param('id', 0, 'trim');

        $buscode = $this->request->param('buscode', '', 'trim');

        $busexpid = $this->request->param('busexpid', '', 'trim');

        $lease = $this->LeaseModel->find($id);

        if (!$lease) {
            $this->error('订单不存在');
        }

        $express = model('Expressquery.Expressquery')->find($busexpid);

        if (!$express) {
            $this->error('物流公司不存在');
        }

        $data = [
            'id' => $id,
            'busexpcode' => $buscode,
            'busexpid' => $busexpid,
            'status' => 4
        ];

        $result = $this->LeaseModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('归还失败');
        } else {
            $this->success('归还成功');
        }
    }

    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');

        $lease = $this->LeaseModel->with(['product'])->find($id);

        if (!$lease) {
            $this->error('订单不存在');
        }

        $this->success('查询成功', null, $lease);
    }

    public function comment()
    {
        $params = $this->request->param();

        $lease = $this->LeaseModel->find($params['id']);

        if (!$lease) {
            $this->error('订单不存在');
        }

        $data = [
            'id' => $params['id'],
            'rate' => $params['rate'],
            'comment' => $params['comment'],
            'status' => 6
        ];

        $result = $this->LeaseModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('评价失败');
        } else {
            $this->success('评价成功');
        }
    }
}
